
<!DOCTYPE html>
<html class="x-admin-sm">
    
    <head>
        <meta charset="UTF-8">
        <title>欢迎页面-X-admin2.2</title>
        <meta name="renderer" content="webkit">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
        <link rel="stylesheet" href="./css/font.css">
        <link rel="stylesheet" href="./css/xadmin.css">
        <link href="js/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet"/>
        <script type="text/javascript" src="./lib/layui/layui.js" charset="utf-8"></script>
        	<!--<script src="js/bootstrap/dist/js/bootstrap.min.js"></script>-->
        	
        <!-- <script type="text/javascript" src="./js/xadmin.js"></script>-->
      <script type="text/javascript" src="js/jquery.min.js" ></script>
      <script type="text/javascript" src="js/jquery.cookie.js"></script>
      
       <link href="./css/materialdesignicons.min.css" rel="stylesheet">
		<link rel="stylesheet" href="./css/materialdesign.css" type="text/css">
		<script type="text/javascript" src="./js/jquery.ztree.all.min.js"></script>
       </head>
    
    <body>
        <div class="layui-fluid">
            <div class="layui-row">
                <form class="layui-form" id="form1">
                    <div class="layui-form-item">
                        <label for="username" class="layui-form-label">
                            <span class="x-red"></span>讲师名称</label>
                       <div class="col-sm-10 has-warning">
                   	<input type="hidden" name="id"  id="id"  />
			<input type="text" autofocus="autofocus" class="form-control" id="name" name="name"  value=""  autocomplete="off" lay-verify="required" placeholder="请输入" />
		    </div>
		    </div>
                      <div class="layui-form-item">
                        <label for="username" class="layui-form-label">
                            <span class="x-red"></span>讲师资历</label>
                       <div class="col-sm-10 has-warning">
			<input type="text" autofocus="autofocus" class="form-control" id="education" name="education"  autocomplete="off"  required="" lay-verify="required" placeholder="请输入" />
		    </div>
		    </div>
              
                    <div class="layui-form-item">
                        <label for="username" class="layui-form-label">
                            <span class="x-red"></span>讲师专业</label>
                       <div class="col-xs-2" onclick="click_ul()" class="divcss5">
		        <input type="text" id="tit_se"   class="form-control" autocomplete="off"  placeholder="请输入" />
				<input type="hidden" id="s" name="subjectId"  class="form-control" autocomplete="off"  placeholder="请输入" />
		<ul id="ztree1" class="ztree"></ul>
		</div>
                    </div>
                    
                    
                      <div class="layui-form-item">
                        <label for="username" class="layui-form-label">
                            <span class="x-red"></span>讲师等级</label>
                        <div class="col-sm-3 ">
			    	<select class="form-control" name="isStar" id="isStar">
			    		<option value="2">首席讲师</option>
			    		<option value="1">高级讲师</option>
			    	</select>
			    </div>
			</div>
			 <div class="layui-form-item">
                        <label for="username" class="layui-form-label">
                            <span class="x-red"></span>讲师排序</label>
                       <div class="col-sm-10 has-warning">
			<input type="text" autofocus="autofocus" class="form-control" id="sort" name="sort" required="" lay-verify="required" placeholder=""  autocomplete="off"/>
		    </div>
		    </div>
		     <!--用来放置图片返回过来的路径，便于新增数据时获取图片地址-->
       <input type="hidden" id="picPath" name="picPath" class="image">
       
    <div class="layui-form-item">
        <label class="layui-form-label ">照片:</label>
        <div class="layui-upload">
            <button type="button" class="layui-btn" name="picPath" id="test1">上传图片</button>
            <div class="layui-upload-list">
                <img class="layui-upload-img" id="demo1" width="200" height="200">
                <p id="demoText"></p>
            </div>
        </div>
    </div>
                  
        <div class="layui-form-item layui-form-text">
            <label for="desc" class="layui-form-label">描述</label>
            <div class="col-sm-10">
				<textarea rows="5" class="form-control" id="career" name="career" lay-verify="required"  autocomplete="off"></textarea>
			</div>
        </div>
        <div class="layui-form-item">
            <label for="L_repass" class="layui-form-label"></label>
            <button class="layui-btn" lay-filter="add" lay-submit="">保存</button>
          <button class="layui-btn-danger layui-btn layui-btn-xs"   href="javascript:;"  id="btn"><i class="layui-icon"></i>退出</button>
  
        </div>
        </form>
        </div>
        </div>
        <script>layui.use(['form', 'layer'],
            function() {
            	$("#id").val($.cookie("id"))
                $ = layui.jquery;
                var form = layui.form,
                layer = layui.layer;

                //自定义验证规则
                form.verify({
                    nikename: function(value) {
                        if (value.length < 5) {
                            return '昵称至少得5个字符啊';
                        }
                    },
                    pass: [/(.+){6,12}$/, '密码必须6到12位'],
                    repass: function(value) {
                        if ($('#L_pass').val() != $('#L_repass').val()) {
                            return '两次密码不一致';
                        }
                    }
                });
                //获取id
           var id=$.cookie("id");

         		 $.ajax({
                            type:"get",
					      	url:"http://localhost:9095/admin/teacher/init",
					      	async:true,
					      	data:{
					      		"id":id
					      	},
					      	headers: {
								"token2": $.cookie("token2")
							},
					      success(msg){
					if(msg.code==200){
                var name=msg.data.name;
                var education=msg.data.education;
                var subjectId=msg.data.subjectId;
                var isStar=msg.data.isStar;
                var sort=msg.data.sort;
                var career=msg.data.career;
                var picPath=msg.data.picPath;
                $("#name").val(name);
                $("#education").val(education);
                $("#sort").val(sort);
                $("#picPath").val(picPath)
                $("#demo1").attr("src",picPath)
                $("#s").val(subjectId);
                $("#career").val(career);
                $("#isStar").val(isStar);
                layui.form.render();
                 $.ajax({
                            type:"get",
					      	url:"http://localhost:9095/admin/teacher/Sysint",
					      	async:true,
					      	data:{
					      		"id":subjectId
					      	},
					      	headers: {
								"token2": $.cookie("token2")
							},
					      success(msg){
					      	var subjectName=msg.data.subjectName;
    	                      $("#tit_se").val(subjectName);
					      }
					      });
                
               }else{
               	alert("查询失败")
               }
					      	 }
					      	});


      	     


                form.on('submit(add)',
                function(data) {
                    $.ajax({
                            type:"post",
					      	url:"http://localhost:9095/admin/teacher/update",
					      	async:true,
					      	data:$("#form1").serialize(),
					      	headers: {
								"token2": $.cookie("token2")
							},
                      success(msg){
                    layer.alert("修改成功", {
                        icon: 6
                    },
                    function() {
                        parent.location.reload();
                    });
                    }
                     });
                    return false;
                });

            });</script>
    <script>
        $(function(){
        	//下拉框
         		var sel1=$("#subjectId");
        		$.get("http://localhost:9095/admin/teacher/Syslist",function(msg){
        			$.each(msg, function(index,obj) {
        				var op=$("<option value='"+obj.subjectId+"'>"+obj.subjectName+"</option>");
        			    sel1.append(op);
        			});
        		});
        		
        		//退出
        		$("#btn").click(function(){
        			// 获得frame索引
                        var index = parent.layer.getFrameIndex(window.name);
                        //关闭当前frame
                        parent.layer.close(index);
        		});
        })
       
        
        	
        	




        
        
        //图片上传
           layui.use('upload', function(){
        	var $ = layui.jquery,
        	upload = layui.upload;
        	
        	//普通图片上传
        	var uploadInst = upload.render({
            	elem: '#test1'
            	,url: 'http://localhost:9095/admin/teacher/upload'
            	,accept:'picPath'
            	,size: 50000
            	,before: function(obj){
                obj.preview(function(index, file, result){
                    $('#demo1').attr('src', result);
                });
            }
            ,done: function(res){
                //如果上传失败
                if(res.code > 0){
                    return layer.msg('上传失败');
                }
                
                //放置图片上传成功后的返回路径
                $("input[name='picPath']").val(res.name)
//              var name = $("input[name='picPath']").val()
              
                //上传成功
                var demoText = $('#demoText');
                demoText.html('<span style="color: #4cae4c;">上传成功</span>');

                var fileupload = $(".image");
                
                console.log(fileupload.attr("value"));
            }
            ,error: function(){
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function(){
                    uploadInst.upload();
                });
            }
        });


    });
        </script>
        
         <script type="text/javascript">
			var zTreeObj;
			var setting = {
				view: {
					enable: true,
					showLine: true,
					removeHoverDom: removeHoverDom,
					
				},
				async: {
					autoParam: ['pid'],
					url: 'http://localhost:9095/sysSubject/selectSysSubject'
				},
				isSimpleData: false, //数据是否采用简单 Array 格式，默认false
				treeNodeKey: "id", //在isSimpleData格式下，当前节点id属性
				treeNodeParentKey: "pId", //在isSimpleData格式下，当前节点的父节点id属性
				showLine: true, //是否显示节点间的连线
				callback: {
					onClick: chooseNode
//					beforeRemove: beforeRemove,
//					onRename: zTreeOnRename
                   
				},
				edit: {
					enable: false, //单独设置为true时，可加载修改、删除图标
				    editNameSelectAll: true,
				},
				data: {
					simpleData: {
						enable: true,
						idKey: "id",
						pIdKey: "pId",
						system: "system",
						rootPId: ""
					}
				},
			};

			$(function() {
				$.ajax({
					async: false,
					cache: false,
					type: 'GET',
					dataType: "json",
					data: {
						pid: ''
					},
					url: "http://localhost:9095/admin/sysSubject/selectSysSubject", //请求的action路径
					
					success: function(msg) { //请求成功后处理函数
						console.log(msg)
						treeNodes = eval(msg.data); //把后台封装好的简单Json格式赋给treeNodes
					}
				});
			});
			
			//初始化节点
			$(document).ready(function() {
				$.fn.zTree.init($("#ztree1"), setting, treeNodes);
			});
			
			
			function removeHoverDom(treeId, treeNode) {
				$("#diyBtn_" + treeNode.id).unbind().remove();
				$("#diyBtn_space_" + treeNode.id).unbind().remove();
				
				
			}
	   
			
			
	 document.getElementById('ztree1').style.display = 'none';
    function click_ul() {                //  点击展开
        document.getElementById('ztree1').style.display = 'block';
        	 
    }
    
   function chooseNode(event,treeId, treeNode) {
      

      $("#s").val(treeNode.id)
     
		var t_obj = event.target;
        document.getElementById('tit_se').value = t_obj.innerHTML;
        setTimeout(function () {
        document.getElementById('ztree1').style.display = 'none';
             
        }, 1);
         
			}
   
        
		</script>
    </body>

</html>
